package com.ruoyi.web.erp.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.czkt.domain.ErpPurchase;
import com.czkt.service.IErpPurchaseService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.web.util.ActivityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 请购单Controller
 * 
 * @author ruoyi
 * @date 2024-05-21
 */
@RestController
@RequestMapping("/system/purchase")
public class ErpPurchaseController extends BaseController
{
    @Autowired
    private IErpPurchaseService erpPurchaseService;

    /**
     * 查询请购单列表
     */
    @PreAuthorize("@ss.hasPermi('system:purchase:list')")
    @GetMapping("/list")
    public TableDataInfo list(ErpPurchase erpPurchase)
    {
        startPage();
        List<ErpPurchase> list = erpPurchaseService.selectErpPurchaseList(erpPurchase);
        return getDataTable(list);
    }

    /**
     * 导出请购单列表
     */
    @PreAuthorize("@ss.hasPermi('system:purchase:export')")
    @Log(title = "请购单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ErpPurchase erpPurchase)
    {
        List<ErpPurchase> list = erpPurchaseService.selectErpPurchaseList(erpPurchase);
        ExcelUtil<ErpPurchase> util = new ExcelUtil<ErpPurchase>(ErpPurchase.class);
        util.exportExcel(response, list, "请购单数据");
    }

    /**
     * 获取请购单详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:purchase:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(erpPurchaseService.selectErpPurchaseById(id));
    }

    @Autowired
    ActivityUtils activityUtils;

    /**
     * 新增请购单
     */
    @PreAuthorize("@ss.hasPermi('system:purchase:add')")
    @Log(title = "请购单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ErpPurchase erpPurchase)
    {

       int addRow= erpPurchaseService.insertErpPurchase(erpPurchase);

        String createBy=SecurityUtils.getUsername();


       //发起流程
       boolean runStatus= activityUtils.runProcess("purchase_audit","purchase_audit_"+erpPurchase.getId(),createBy,createBy);
//        //完成发起审批节点
//        if(runStatus){
//            activityUtils.completeTask("purchase_audit_"+erpPurchase.getId(),"发起请购审批",createBy);
//        }
        return toAjax(addRow);
    }

    /**
     * 修改请购单
     */
    @PreAuthorize("@ss.hasPermi('system:purchase:edit')")
    @Log(title = "请购单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ErpPurchase erpPurchase)
    {
        return toAjax(erpPurchaseService.updateErpPurchase(erpPurchase));
    }

    /**
     * 删除请购单
     */
    @PreAuthorize("@ss.hasPermi('system:purchase:remove')")
    @Log(title = "请购单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(erpPurchaseService.deleteErpPurchaseByIds(ids));
    }




    /**
     * 获取请购单流程
     */
    @PreAuthorize("@ss.hasPermi('system:purchase:processImage')")
    @GetMapping(value = "/processImage/{id}")
    public AjaxResult processImage(@PathVariable("id") Long id)
    {
        AjaxResult rs= new AjaxResult();

        try {
            String img =  activityUtils.createImgBase64("purchase_audit","purchase_audit_"+id);
            rs.put("data",img);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  rs;

    }
}
